package com.coolgor.axnut.service.mdmis.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.mdmis.PolicyDao;
import com.coolgor.axnut.dao.mdmis.PolicyImportDao;
import com.coolgor.axnut.dto.mdmis.PolicyExecution;
import com.coolgor.axnut.entity.mdmis.Policy;
import com.coolgor.axnut.entity.mdmis.PolicyCondition;
import com.coolgor.axnut.enums.mdmis.PolicyOperationStateEnum;
import com.coolgor.axnut.exceptions.mdmis.PolicyOperationException;
import com.coolgor.axnut.service.mdmis.PolicyService;
import com.coolgor.axnut.util.PageCalculator;

@Service
public class PolicyServiceImpl implements PolicyService {
	private static Logger log = LoggerFactory.getLogger(PolicyServiceImpl.class);
	
	@Autowired
	private PolicyDao policyDao;	
	@Autowired
	private PolicyImportDao policyImportDao;


	@Override
	public Policy getPolicy(String insureNo) {
		return policyDao.queryPolicyByInsuerNo(insureNo);
	}

	@Override
	public PolicyExecution getPolicyList(List<String> insureNoList, int pageIndex, int pageSize) {
		if ( insureNoList==null || insureNoList.size()<1 ) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = policyDao.queryPolicyCountByInsureNo(insureNoList);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Policy> list = policyDao.queryPolicyListByInsureNo(insureNoList,rowIndex,pageSize);
		PolicyExecution result = new PolicyExecution();
		if (list != null) {
			result.setState(PolicyOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(PolicyOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(PolicyOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}
	
	@Override
	public PolicyExecution getPolicyList(Policy policyCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Policy> list = policyDao.queryPolicyList(policyCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = policyDao.queryPolicyCount(policyCondition);
		PolicyExecution result = new PolicyExecution();
		if (list != null) {
			result.setState(PolicyOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(PolicyOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(PolicyOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}	
	
	@Override
	public PolicyExecution getPolicyList(PolicyCondition policyCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Policy> list = policyDao.queryPolicyListByCondition(policyCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = policyDao.queryPolicyCountByCondition(policyCondition);
		PolicyExecution result = new PolicyExecution();
		if (list != null) {
			result.setState(PolicyOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(PolicyOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(PolicyOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public PolicyExecution addPolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = policyDao.insertPolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息创建失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("add policy Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution modifyPolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = policyDao.updatePolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息更新失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Modify policy Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution removePolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyDao.deletePolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Delete policy Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution batchRemovePolicy(List<Policy> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		List<String> insureNoList = new ArrayList<String>();
		for (int i=0; i<list.size(); i++) {
			insureNoList.add(list.get(i).getInsureNo());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyDao.batchDeletePolicy(insureNoList);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Batch delete Policy Information error:" + e.getMessage());
		}
		PolicyExecution ce = new PolicyExecution(PolicyOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		ce.setCount(effectedNum);
		return ce;
	}

	@Override
	public Policy getTempPolicy(String insureNo) {
		return policyImportDao.queryPolicyByInsuerNo(insureNo);
	}

	@Override
	public PolicyExecution getTempPolicyList(Policy policyCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<Policy> list = policyImportDao.queryPolicyList(policyCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = policyImportDao.queryPolicyCount(policyCondition);
		PolicyExecution result = new PolicyExecution();
		if (list != null) {
			result.setState(PolicyOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(PolicyOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(PolicyOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	public PolicyExecution addTempPolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = policyImportDao.insertPolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息创建失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("add policyImport Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution modifyTempPolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = policyImportDao.updatePolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单信息更新失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Modify policyimport Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution removeTempPolicy(Policy policy) {
		// 空值判断
		if (policy == null) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyImportDao.deletePolicy(policy);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单临时信息删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Delete policyimport Information error:" + e.getMessage());
		}
		return new PolicyExecution(PolicyOperationStateEnum.SUCCESS, policy);
	}

	@Override
	@Transactional
	public PolicyExecution batchRemoveTempPolicy(List<Policy> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new PolicyExecution(PolicyOperationStateEnum.NULL);
		}
		List<String> insureNoList = new ArrayList<String>();
		for (int i=0; i<list.size(); i++) {
			insureNoList.add(list.get(i).getInsureNo());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyImportDao.batchDeletePolicy(insureNoList);
			if (effectedNum <= 0) {
				throw new PolicyOperationException("保单临时信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyOperationException("Batch delete PolicyImport Information error:" + e.getMessage());
		}
		PolicyExecution ce = new PolicyExecution(PolicyOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		ce.setCount(effectedNum);
		return ce;
	}


}
